package top.wuyang.trywork.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.wuyang.trywork.base.PageBean;
import top.wuyang.trywork.base.R;
import top.wuyang.trywork.dao.TbSoftwareMapper;
import top.wuyang.trywork.dao.TbSoftwareTypeMapper;
import top.wuyang.trywork.entity.TbSoftware;
import top.wuyang.trywork.entity.TbSoftwareType;
import top.wuyang.trywork.service.TbSoftwareService;
import top.wuyang.trywork.vo.PageVO;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbSoftwareServiceImpl implements TbSoftwareService {

    private final TbSoftwareTypeMapper tbSoftwareTypeMapper;
    private final TbSoftwareMapper tbSoftwareMapper;

    @Override
    public R<String> insert(TbSoftware tbSoftware) {
        TbSoftwareType check = tbSoftwareTypeMapper.selectById(tbSoftware.getStid());
        if (check == null) {
            return R.fail("该类型不存在", null);
        }
        int i = tbSoftwareMapper.insert(tbSoftware);
        return i == 1 ? R.ok("新增成功", null) : R.fail("新增失败", null);
    }

    @Override
    public R<PageVO<TbSoftware>> query(TbSoftware tbSoftware, PageBean pageBean){
        IPage<TbSoftware> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
        QueryWrapper<TbSoftware> wrapper = new QueryWrapper<>();
        wrapper.eq(tbSoftware.getStid() != null,"stid",tbSoftware.getStid());
        wrapper.like(StringUtils.hasText(tbSoftware.getSname()),"sname",tbSoftware.getSname());
        page= tbSoftwareMapper.selectPage(page, wrapper);
        return R.ok(page);
    }

    @Override
    public R<TbSoftware> queryById(Integer id) {
        TbSoftware tbSoftware = tbSoftwareMapper.selectById(id);
        return tbSoftware == null ? R.fail("主键不存在", null) : R.ok("查询成功", tbSoftware);
    }

    @Override
    public R<String> update(TbSoftware tbSoftware) {
        TbSoftwareType check = tbSoftwareTypeMapper.selectById(tbSoftware.getStid());
        if (check == null) {
            return R.fail("该类型不存在", null);
        }
        int i = tbSoftwareMapper.updateById(tbSoftware);
        return i == 1 ? R.ok("修改成功", null) : R.fail("修改失败", null);
    }

    @Override
    public R<String> delete(Integer id) {
        int i = tbSoftwareMapper.deleteById(id);
        return i == 1 ? R.ok("删除成功", null) : R.fail("删除失败", null);
    }



}
